package com.bd.scm.module.item.service.impl;

import com.bd.scm.dao.mapper.gly.GlyMapper;
import com.bd.scm.dao.mapper.item.ItemMapper;
import com.bd.scm.domain.dto.item.ItemDto;
import com.bd.scm.domain.models.gly.Gly;
import com.bd.scm.domain.models.item.Item;
import com.bd.scm.module.common.BaseServiceImpl;
import com.bd.scm.module.enums.CodeRuleEnum;
import com.bd.scm.module.item.service.ItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: _Cps
 * Date: 2019.12.21 11:20
 */
@Service
public class ItemServiceImpl extends BaseServiceImpl<ItemMapper, Item> implements ItemService {

    @Resource
    private ItemMapper itemMapper;


    @Override
    public List<Item> getItemByTypeId(ItemDto itemDto) {
        return itemMapper.getItemByTypeId(itemDto);
    }

    /**
     * 新增或修改班次类型.
     *
     * @param item
     * @return
     */
    @Override
    public String addOrEdit(Item item) {
        //返回不为空，返回信息就是错误原因
        String msg = null;
        //判断成本中心名称是否重复
        Integer i = itemMapper.getCountByName(item.getName());
        msg = i > 0 ? "班次类型名称已存在！" : null;
        if (null == msg) {
            if (null != item.getId()){
                //编辑
                itemMapper.editBanciType(item);
            }else {
                //新增
                //获取id和bh
                Integer id = getMaxId(CodeRuleEnum.BANCITYPE);
                String bh = getNumber(CodeRuleEnum.BANCITYPE);
                item.setId(id);
                item.setBh(bh);
                itemMapper.addBanciType(item);
            }
        }
        return msg;
    }

    /**
     * 删除班次类型.
     *
     * @param item
     * @return
     */
    @Override
    public String deleteBanciType(Item item) {
        //返回不为空，返回信息就是错误原因
        String msg = null;
        //判断班次类型名称是否重复
        Integer i = itemMapper.getCountOfBanci(item);
        msg = i > 0 ? "班次类型已有班次绑定，无法删除！" : null;
        if (null == msg) {
            itemMapper.deleteBanciType(item);
        }
        return msg;
    }
}
